package org.wingstudio.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.wingstudio.entity.News;
import org.wingstudio.entity.PageBean;
import org.wingstudio.entity.Source;
import org.wingstudio.lucene.FileIndex;
import org.wingstudio.lucene.NewsIndex;
import org.wingstudio.lucene.SourceIndex;
import org.wingstudio.service.FileService;
import org.wingstudio.service.NewsService;
import org.wingstudio.util.PageUtil;
import org.wingstudio.util.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游客预览文件，下载文件模块
 * Created by liao on 16-11-19.
 */

@Controller
@RequestMapping("/")
public class NewsNormalController {

    @Resource
    private NewsService newsService;

    private FileIndex fileIndex=new FileIndex();
    private NewsIndex newsIndex=new NewsIndex();
    private SourceIndex sourceIndex=new SourceIndex();
    @RequestMapping("/news/{id}")
    public ModelAndView news(@PathVariable("id")Integer id, HttpServletRequest request)
            throws Exception{
        ModelAndView modelAndView=new ModelAndView();

        News news=newsService.findById(id);
        news.setClickHit(news.getClickHit()+1);
        newsService.update(news);
        modelAndView.addObject("news",news);
        modelAndView.setViewName("user/newsView/viewNews");
        return modelAndView;
    }

    @RequestMapping("/work")
    public ModelAndView work(@RequestParam(value="page",required=false)String page,
                             HttpServletRequest request)
            throws Exception{

        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("newsTypeId",1);
        List<News> newsList=newsService.list(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/work",
                newsService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("workList",newsList);
        modelAndView.setViewName("user/news/work");

        return modelAndView;
    }

    @RequestMapping("/service")
    public ModelAndView service(@RequestParam(value="page",required=false)String page,
                             HttpServletRequest request)
            throws Exception{

        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("newsTypeId",2);
        List<News> newsList=newsService.list(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/service",
                newsService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("serviceList",newsList);
        modelAndView.setViewName("user/news/service");

        return modelAndView;
    }

    @RequestMapping("/tender")
    public ModelAndView tender(@RequestParam(value="page",required=false)String page,
                                HttpServletRequest request)
            throws Exception{

        ModelAndView modelAndView=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("newsTypeId",3);
        List<News> newsList=newsService.list(map);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/tender",
                newsService.getTotal(map), Integer.parseInt(page), 10));
        modelAndView.addObject("tenderList",newsList);
        modelAndView.setViewName("user/news/tender");

        return modelAndView;
    }

    @RequestMapping("/search")
    public ModelAndView search(HttpServletRequest request)throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String content=request.getParameter("content");
        List<News> newsList=newsIndex.searchNews(content);
        List<org.wingstudio.entity.File> fileList=fileIndex.searchFile(content);
        List<Source> sourceList=sourceIndex.searchSource(content);


        modelAndView.addObject("sourceList",sourceList);
        modelAndView.addObject("fileList",fileList);
        modelAndView.addObject("newsList",newsList);
        modelAndView.setViewName("user/searchResult");
        return modelAndView;
    }
}
